# 查出前十的人气店
import base64
import json
import time
import zlib

import execjs
import requests
from lxml import etree


headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36",
    "Cookie":"fspop=test; _lxsdk_cuid=18ffb26b5fdc8-0b9471ca33e17-26001c51-144000-18ffb26b5fdc8; _lxsdk=18ffb26b5fdc8-0b9471ca33e17-26001c51-144000-18ffb26b5fdc8; _hc.v=0eb2dab6-dec9-b1f2-b49e-e2045daa016d.1717905569; Hm_lvt_602b80cf8079ae6591966cc70a3940e7=1717905570; SL_G_WPT_TO=en; SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; WEBDFPID=32020u0x1wux5882104w766u50uz89z481uuy73v1y7979583z7y5487-2033265581848-1717905581848SCWAYIYfd79fef3d01d5e9aadc18ccd4d0c95073412; dper=02020a22b73a1882858f4630769a6ca017a09d2113663e671bf19950cc34f10c64712fe001c70fbf7fd3961f92acad01cf29a5d68ad1e896f16d00000000a6200000dd4f75560771b09cfb87f9cf8e5d0035809e0b30274c89790a68c35f23fe5e4307ef3abf9e8a775a9ffd4685dd52030e; qruuid=e11f6ee8-3a0b-4f20-a8fb-f1e8ed75b0b4; ll=7fd06e815b796be3df069dec7836c3df; s_ViewType=10; aburl=1; cy=5; cye=nanjing; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; Hm_lpvt_602b80cf8079ae6591966cc70a3940e7=1717909951; _lxsdk_s=18ffb26b5fd-641-bf4-c71%7C%7C3094"
}

url = 'https://www.dianping.com/tangshan/ch10/g34236o2'

#发送请求获取前十店铺

def top10():
    get = requests.get(url, headers=headers)
    text = get.text
    html = etree.HTML(text)
    coupon_urls = html.xpath("//*[@id='shop-all-list']/ul/li/div[@class='pic']/a/@href")
    xpath = html.xpath("//*[@id='shop-all-list']/ul/li/div/a/img/@title")
    print(xpath[0:10])
    print(coupon_urls[0:10])
    print(len(xpath[0:10]))
    return coupon_urls[0:10]

# top10()
def token():
    jw = 'eJxVj1FvgjAUhf/LfSbQYotK4kONcYMM47QQt8UHrAjMUJRWYZj999XMPezpnPvdc5J7b9AEe/AxQohgC65ZAz5gG9keWKCV2QzxcEwHwwHBA88C8Y9RhKkFuyaZgf+BqQlgirZ3sjLgl4w8srUe1jXWJdZdYBeYCBRan5TvOG3b2vsyladS5raoK0cV9cl5Js1lM5XzVznLI7GZm5vAVCtuqkaPD00fqv/myDxhsqrMpXFZ2PG1Iup8WEWKx1+J0Is+6npediEXoyurj29Vl79XnkpomMm5rtJzkUg6FWzKcVQe60+5fBHsuhBIujwY94xIL7yklcu9wyLbP/Uz0S7P6y7QPEYxC1hXR8WSTSbw/QOoEGg0'

    jw = base64.b64decode(jw)
    # 解码后的参数进行解压转换为字符串
    jw = str(zlib.decompress(jw), 'utf-8')
    # 将字符串转换为表达式
    jw = eval(jw)
    # 将jw中的cts参数替换
    jw['cts'] = int(time.time() * 1000)
    # 编码字符串
    jw = str(jw).encode()
    # 将jw进行base64编码
    token = base64.b64encode(zlib.compress(jw)).decode()
    # print(str(token))

    return token

f = open("AST/dzdpnx.js", mode='r', encoding="utf-8")
js_code = f.read()
f.close()

js = execjs.compile(js_code)
ret = js.call("he")

print(ret)
def getCoupon():
    url = 'https://www.dianping.com/ajax/json/shopDynamic/promoInfo'

    params = {
        'shopId': 'k6JLHcF90aI4bNuN',
        'cityId': '25',
        'shopName': '喜茶',
        'power': '5',
        'mainCategoryId': '34237',
        'shopType': '10',
        '_token': token(),
        'uuid': '94113368-0ada-ab80-224f-fb9011e24174.1718726115',
        'platform': '1',
        'partner': '150',
        'optimusCode': '10',
        'originUrl': 'https://www.dianping.com/shop/H4ruXBnFQnDgMcXF',
        'yodaReady': 'h5',
        'csecplatform': '4',
        'csecversion': '2.4.0',
        'mtgsig': ret
    }
    print(params)
    coupon_get = requests.get(url,params= params, headers=headers)
    print(coupon_get.text)
    # print(json.loads(coupon_get.text)['dealMoreDetails'])


# 获取优惠券
getCoupon()
# t = 'eJxVj1FvgjAUhf9Lnxtosa1I4kONcYMM47QSt8UHrAjMUJRWYZj999XMPezpnPvdc5J7b6AJ9yDACCGCIbhmDQgAdpDDAARG280QD0d04PvEZxQC+Y9RnyAIdk0yBcEHpgMGMUXbO1la8Et8RrbwYT1rPQLvAnahjYDCmJMOXLdtW2dfpupUqtyRdeXqoj65z6S5bCZq9qqmeSw3M3sTsNVK2KrV40PTh5q/ObZP2Kwuc2VdFnVipYk+H5axFuuvRJp5H3e9KLtISP/K6+Nb1eXvFdMJjTI1M1V6LhJFJ5JPBI7LY/2pFi+SX+cSKU+Eo54TxaJLWnmCHebZ/qmfynZxXnWhEWu05iHv6rhY8PEYfP8AyA1oTw=='
# print(t)
# gura = {
#     'SCaApp' :  'false',
#     'data' : 'null',
#     'headers' : {'Accept': 'application/json, text/javascript, */*; q=0.01', 'X-Requested-With': 'XMLHttpRequest'},
#     'isRaptor' : 'false',
#     'method' : 'GET',
#     'openArg' : ['GET', '"https://www.dianping.com/ajax/json/shopDynamic/promoInfo?shopId=H4ruXBnFQnDgMcXF&cityId=25&shopName=%E5%96%9C%E8%8C%B6&power=5&mainCategoryId=34237&shopType=10&_token='
#                 +t+ '&uuid=0eb2dab6-dec9-b1f2-b49e-e2045daa016d.1717905569&platform=1&partner=150&optimusCode=10&originUrl=https%3A%2F%2Fwww.dianping.com%2Fshop%2FH4ruXBnFQnDgMcXF&yodaReady=h5&csecplatform=4&csecversion=2.4.0"',  'true', 'undefined', 'undefined'],
#     'openHookedCount' : 1,
#     'oriUrl' :'https://www.dianping.com/ajax/json/shopDynamic/promoInfo?shopId=H4ruXBnFQnDgMcXF&cityId=25&shopName=%E5%96%9C%E8%8C%B6&power=5&mainCategoryId=34237&shopType=10&_token=' +
#               t
#               +'&uuid=0eb2dab6-dec9-b1f2-b49e-e2045daa016d.1717905569&platform=1&partner=150&optimusCode=10&originUrl=https%3A%2F%2Fwww.dianping.com%2Fshop%2FH4ruXBnFQnDgMcXF',
#     'sendHookedCount':1,
#     'signType':2,
#     'url':'https://www.dianping.com/ajax/json/shopDynamic/promoInfo?shopId=H4ruXBnFQnDgMcXF&cityId=25&shopName=%E5%96%9C%E8%8C%B6&power=5&mainCategoryId=34237&shopType=10&_token='
#             + t         + '&uuid=0eb2dab6-dec9-b1f2-b49e-e2045daa016d.1717905569&platform=1&partner=150&optimusCode=10&originUrl=https%3A%2F%2Fwww.dianping.com%2Fshop%2FH4ruXBnFQnDgMcXF&yodaReady=h5&csecplatform=4&csecversion=2.4.0'
# }


